package main

import (
	"fmt"
	ping "gee/day5-middleware/Ping"
	"log"
	"net/http"
	"time"
)

func onlyForV2() ping.HandlerFunc {
	return func(c *ping.Context) {
		t := time.Now()
		//c.String(http.StatusInternalServerError, "<h1>500</h1>")
		log.Printf("[%d] %s in %v for group v2", c.StatusCode, c.Req.RequestURI, time.Since(t))
	}
}

func main() {
	p := ping.New()
	p.UseBefore(ping.Logger())
	p.GET("/index", func(c *ping.Context) {
		c.Json(http.StatusOK, ping.H{
			"code":    200,
			"message": "ok",
			"data": ping.H{
				"name": "ping",
				"age":  18,
			},
		})
	})

	v1 := p.Group("/v2")
	v1.UseBefore(onlyForV2())
	{
		v1.GET("/list", func(c *ping.Context) {
			//c.HTML(http.StatusOK, "<h1>list</h1>")
			fmt.Println("=====/list=====")
		})
		v1.GET("/info", func(c *ping.Context) {
			c.String(http.StatusOK, "<h1>list</h1>")
		})
	}
	p.Run(":9999")
}
